class_name BigHandler extends BaseHandler

func update_big(data:Dictionary):
	var player = gameManager.map_players.get(data.player_username) as Player
	var big = player.map_bigs.get(data.id)
	if big != null:
		big.update(data)

func born_big(data:Dictionary):
	var point_id = data.point_id
	var point = gameManager.map_points.get(point_id)
	var big = gameManager.pre_big.instantiate()
	gameManager.layer_chesses.add_child(big)
	big.init(data,point)
	# 玩家是自己或者观战复盘
	if big.player.is_me || ObjectManager.gameManager.me_player == null:
		ObjectManager.soundManager.play(SoundType.big_born)


func big_move(data:Dictionary):
	var point_start = gameManager.map_points.get(data.start)
	var point_end = gameManager.map_points.get(data.end)
	var big = point_start.big as Big
	big.moveBefore()
	point_end.big = big
	point_start.big = null
	big.point = point_end
	big.position = point_end.position
	# 玩家是自己或者观战复盘
	if big.player.is_me || ObjectManager.gameManager.me_player == null:
		ObjectManager.soundManager.play(SoundType.big_move)

# 其实就是大子移动，为了播放音效拆一个方法出来
func big_attack(data:Dictionary):
	var point_start = gameManager.map_points.get(data.start)
	var point_end = gameManager.map_points.get(data.end) as Point
	var big = point_start.big as Big
	big.moveBefore()
	point_end.big = big
	point_start.big = null
	big.point = point_end
	big.position = point_end.position
	# 玩家是自己或者观战复盘
	if point_end.is_view:
		ObjectManager.soundManager.play(SoundType.big_attack)
		gameManager.layer_effect.bigAttack(point_end)

# 转型
func evolve(data:Dictionary):
	var player = gameManager.map_players.get(data.player_username)
	var old_big = player.map_bigs.get(data.id)
	var new_big:Big
	match data.evolution:
		"幽灵":
			new_big = gameManager.pre_幽灵.instantiate()
		"恶灵":
			new_big = gameManager.pre_恶灵.instantiate()
		"炮台":
			new_big = gameManager.pre_炮台.instantiate()
		"狙炮":
			new_big = gameManager.pre_狙炮.instantiate()
		"顽石":
			new_big = gameManager.pre_顽石.instantiate()
		"地灵":
			new_big = gameManager.pre_地灵.instantiate()
		"精灵":
			new_big = gameManager.pre_精灵.instantiate()
		"沙兵":
			new_big = gameManager.pre_沙兵.instantiate()
		"沙皇":
			new_big = gameManager.pre_沙皇.instantiate()
		"野猪":
			new_big = gameManager.pre_野猪.instantiate()
		"野马":
			new_big = gameManager.pre_野马.instantiate()
		"拳师":
			new_big = gameManager.pre_拳师.instantiate()
		"灵阵":
			new_big = gameManager.pre_灵阵.instantiate()
		"天谕":
			new_big = gameManager.pre_天谕.instantiate()
		"妖术":
			new_big = gameManager.pre_妖术.instantiate()
		"鼓舞":
			new_big = gameManager.pre_鼓舞.instantiate()
	gameManager.layer_chesses.add_child(new_big)
	new_big.evolve(old_big)
	old_big.die()
	# 玩家是自己或者观战复盘
	if new_big.point.is_view:
		ObjectManager.soundManager.play(SoundType.big_evolve)

func big_die(data:Dictionary):
	var point = gameManager.map_points.get(data.point_id)
	var big = point.big
	var isMurderer = false
	if gameManager.me_player:
		isMurderer = data.murderer == gameManager.me_player.username
	if data.is_show_effect:
		var effect_point = gameManager.map_points.get(data.effect_point_id)
		if effect_point != null:
			gameManager.layer_effect.bigDie(effect_point, big.player, isMurderer)
		else:
			gameManager.layer_effect.bigDie(point, big.player, isMurderer)
	big.player.map_bigs.erase(big.id)
	point.big = null
	big.die()

# 被附身
func big_possess(data:Dictionary):
	var point = gameManager.map_points.get(data.point_id)
	var big = point.big
	var old_player = big.player as Player
	old_player.map_bigs.erase(big.id)
	var player = gameManager.map_players.get(data.player_username) as Player
	big.player = player
	player.map_bigs.merge({big.id: big})
	if data.father_id != null:
		var new_father_big = player.map_bigs.get(data.father_id)
		big.father_big = new_father_big
	big.mother_small = player.small
	big.possess()
	# 玩家是自己或者观战复盘
	if old_player.is_me || player.is_me || gameManager.me_player == null:
		ObjectManager.soundManager.play(SoundType.big_evolve)
